﻿using System.Collections.Generic;

namespace SwParaPlatform2
{
    public class DictionaryConverter
    {
        /// <summary>
        /// 将嵌套的 Dictionary<string, Dictionary<string, object>> 转换为 Dictionary<string, object> 类型。
        /// </summary>
        /// <param name="nestedDict">嵌套字典</param>
        /// <returns>转换后的 Dictionary<string, object> 类型</returns>
        public static Dictionary<string, object> ConvertToNestedObjectDict(
            Dictionary<string, Dictionary<string, object>> nestedDict)
        {
            var result = new Dictionary<string, object>();

            foreach (var kvp in nestedDict)
            {
                if (kvp.Value is Dictionary<string, object> innerDict)
                {
                    result[kvp.Key] = innerDict; // 直接添加内层字典
                }
                else
                {
                    result[kvp.Key] = kvp.Value;
                }
            }

            return result;
        }

        /// <summary>
        /// 通用的递归转换方法，将嵌套字典的所有层级转换为 Dictionary<string, object>。
        /// </summary>
        /// <param name="dict">任意层级的字典</param>
        /// <returns>转换后的字典</returns>
        public static Dictionary<string, object> ConvertToObjectDictRecursive(
            Dictionary<string, object> dict)
        {
            var result = new Dictionary<string, object>();

            foreach (var kvp in dict)
            {
                if (kvp.Value is Dictionary<string, object> nestedDict)
                {
                    // 递归转换子字典
                    result[kvp.Key] = ConvertToObjectDictRecursive(nestedDict);
                }
                else
                {
                    result[kvp.Key] = kvp.Value;
                }
            }

            return result;
        }
    }

}
